package com.kpmg.datalake.common.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.kpmg.datalake.common.dao.ExecuteMapper;
import com.kpmg.datalake.common.dynamicdatasource.DynamicDS;
import com.kpmg.datalake.common.service.CommonExecuteService;
import com.kpmg.datalake.common.utils.SqlParser;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Alex.zhao [alex.zhao@kpmg.com]
 * @since Created on azhao6 on 2018-06-27 14:52:18
 */
@Service
public class CommonExecuteServiceImpl implements CommonExecuteService {


  @Resource
  private ExecuteMapper executeMapper;


  /**
   * 通用查询接口
   *
   * @param databaseId 数据源ID
   * @param selectSql 查询sql
   * @return 查询结果
   */
  @Override
  @DynamicDS
  public List<Map<String, Object>> queryListByString(String databaseId, String selectSql) {
    return executeMapper.queryListByString(selectSql);
  }

  /**
   * 通用分页查询接口
   *
   * @param databaseId 数据源ID
   * @param page 分页对象
   * @param selectSql 查询sql
   * @return 查询结果
   */
  @Override
  @DynamicDS
  public Page<Map<String, Object>> queryPageByString(String databaseId,
      Page<Map<String, Object>> page, String selectSql) {
    page.setRecords(executeMapper.queryPageByString(page, selectSql));
    return page;
  }

  /**
   * 通用查询接口
   *
   * @param databaseId 数据源ID
   * @param selectSql 查询sql
   * @param params 查询参数，必须含有查询sql，查询sql的key为selectSql
   * @return 查询结果
   */
  @Override
  @DynamicDS
  public List<Map<String, Object>> queryListByMap(String databaseId, String selectSql,
      Map<String, Object> params) {
    params.put("selectSql", SqlParser.parse(selectSql, params));
    return executeMapper.queryListByMap(params);
  }

  /**
   * 通用分页查询接口
   *
   * @param databaseId 数据源ID
   * @param page 分页对象
   * @param selectSql 查询sql
   * @param params 查询参数，必须含有查询sql，查询sql的key为selectSql
   * @return 查询结果
   */
  @Override
  @DynamicDS
  public Page<Map<String, Object>> queryPageByMap(String databaseId,
      Page<Map<String, Object>> page, String selectSql, Map<String, Object> params) {
    params.put("selectSql", SqlParser.parse(selectSql, params));
    page.setRecords(executeMapper.queryPageByMap(page, params));
    return page;
  }

  /**
   * 通过新增接口
   *
   * @param databaseId 数据源ID
   * @param insertSql 新增sql
   * @return 影响行数
   */
  @Override
  @DynamicDS
  @Transactional(rollbackFor = Exception.class)
  public Integer insertByString(String databaseId, String insertSql) {
    return executeMapper.insertByString(insertSql);
  }

  /**
   * 通过新增接口
   *
   * @param databaseId 数据源ID
   * @param insertSql 新增sql
   * @param params 查询参数，必须含有更新sql，更新sql的key为insertSql
   * @return 影响行数
   */
  @Override
  @DynamicDS
  @Transactional(rollbackFor = Exception.class)
  public Integer insertByMap(String databaseId, String insertSql, Map<String, Object> params) {
    params.put("insertSql", SqlParser.parse(insertSql, params));
    return executeMapper.insertByMap(params);
  }

  /**
   * 通过更新接口
   *
   * @param databaseId 数据源ID
   * @param updateSql 跟新sql
   * @return 影响行数
   */
  @Override
  @DynamicDS
  @Transactional(rollbackFor = Exception.class)
  public Integer updateByString(String databaseId, String updateSql) {
    return executeMapper.updateByString(updateSql);
  }

  /**
   * 通过更新接口
   *
   * @param databaseId 数据源ID
   * @param updateSql 跟新sql
   * @param params 查询参数，必须含有更新sql，更新sql的key为updateSql
   * @return 影响行数
   */
  @Override
  @DynamicDS
  @Transactional(rollbackFor = Exception.class)
  public Integer updateByMap(String databaseId, String updateSql, Map<String, Object> params) {
    params.put("updateSql", SqlParser.parse(updateSql, params));
    return executeMapper.updateByMap(params);
  }

  /**
   * 通用删除sql
   *
   * @param databaseId 数据源ID
   * @param deleteSql 删除sql
   * @return 影响行数
   */
  @Override
  @DynamicDS
  @Transactional(rollbackFor = Exception.class)
  public Integer deleteByString(String databaseId, String deleteSql) {
    return executeMapper.deleteByString(deleteSql);
  }

  /**
   * 通用删除sql
   *
   * @param databaseId 数据源ID
   * @param deleteSql 删除sql
   * @param params 查询参数，必须含有删除sql，删除sql的key为deleteSql
   * @return 影响行数
   */
  @Override
  @DynamicDS
  @Transactional(rollbackFor = Exception.class)
  public Integer deleteByString(String databaseId, String deleteSql, Map<String, Object> params) {
    params.put("deleteSql", SqlParser.parse(deleteSql, params));
    return executeMapper.deleteByMap(params);
  }

  /**
   * 执行存储过程
   *
   * @param databaseId 数据源ID
   * @param callSql 存储过程对象
   * @return Map 返回结果集
   */
  @Override
  @DynamicDS
  @Transactional(rollbackFor = Exception.class)
  public Map<String, Object> callByString(String databaseId, String callSql) {
    Map<String, Object> params = new HashMap<>(16);
    params.put("callSql", SqlParser.parse(callSql, params));
    executeMapper.callByMap(params);
    return params;
  }

}
